package com.chii.gateway.admin.controller;


import com.chii.gateway.admin.client.GatewayClient;
import com.chii.gateway.admin.po.GatewayRoute;
import com.chii.gateway.admin.po.GatewayRouteForm;
import com.chii.gateway.admin.po.ResponeInfo;
import com.chii.gateway.admin.service.IGatewayRouteService;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/gateway/routes")
@Api("gateway routes")
public class GatewayRouteController {
    private Logger logger = LogManager.getLogger(GatewayRouteController.class);

    @Autowired
    private IGatewayRouteService gatewayRoutService;
    @Autowired
    private GatewayClient gatewayClient;

    @ApiOperation(value = "新增网关路由", notes = "新增一个网关路由")
    @ApiImplicitParam(name = "gatewayRout", value = "新增网关路由form表单", required = true, dataType = "GatewayRoute")
    @PostMapping
    public ResponeInfo add(@Valid @RequestBody GatewayRouteForm gatewayRouteForm) {
        ResponeInfo responeInfo = new ResponeInfo();

        logger.info("name:" + gatewayRouteForm);
        GatewayRoute gatewayRout = gatewayRouteForm.toPo(GatewayRoute.class);
        responeInfo.success(gatewayRoutService.add(gatewayRout));
        return responeInfo;
    }

    @ApiOperation(value = "删除网关路由", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public ResponeInfo delete(@PathVariable String id) {
        ResponeInfo responeInfo = new ResponeInfo();
        responeInfo.success(gatewayRoutService.delete(id));
        return responeInfo;
    }

    @ApiOperation(value = "修改网关路由", notes = "修改指定网关路由信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "网关路由ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "gatewayRout", value = "网关路由实体", required = true, dataType = "GatewayRoute")
    })
    @PutMapping(value = "/{id}")
    public ResponeInfo update(@PathVariable Integer id, @Valid @RequestBody GatewayRouteForm gatewayRouteForm) {
        ResponeInfo responeInfo = new ResponeInfo();
        GatewayRoute gatewayRout = gatewayRouteForm.toPo(GatewayRoute.class);
        responeInfo.success(gatewayRoutService.update(gatewayRout));
        return responeInfo;
    }

    @ApiOperation(value = "获取网关路由", notes = "根据id获取指定网关路由信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "网关路由ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public ResponeInfo get(@PathVariable String id) {
        ResponeInfo responeInfo = new ResponeInfo();

        logger.info("get with id:{}", id);
        responeInfo.success(new GatewayRouteForm(gatewayRoutService.get(id)));
        return responeInfo;
    }


    @ApiOperation(value = "重载网关路由", notes = "将所以网关的路由全部重载到redis中")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = ResponeInfo.class)
    )
    @PostMapping(value = "/overload")
    public ResponeInfo overload() {
        ResponeInfo responeInfo = new ResponeInfo();
        responeInfo.success(gatewayRoutService.overload());
        return responeInfo;
    }

    @GetMapping(value = "/test")
    public ResponeInfo test() {
        ResponeInfo responeInfo = new ResponeInfo();
        gatewayClient.add("test", "{\"predicates\":[{\"name\":\"Path\",\"args\":{\"pattern\":\"/color/**\"}}],\"filters\":[{\"name\":\"StripPrefix\",\"args\":{\"parts\":\"1\"}}],\"routeId\":\"xx_routeid\",\"uri\":\"lb://sc-color\"}");
        gatewayClient.refresh();
        return responeInfo;
    }

    @GetMapping(value = "/test2")
    public ResponeInfo test2() {
        ResponeInfo responeInfo = new ResponeInfo();

        responeInfo.success(gatewayClient.get());
        return responeInfo;
    }


}